﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.ServiceModel;
using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface IServiceTest
    {
        [OperationContract]
        void AddRecord();

        [OperationContract]
        IEnumerable<Received> SelRecord();
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ServiceTest" in code, svc and config file together.
	// NOTE: In order to launch WCF Test Client for testing this service, please select ServiceTest.svc or ServiceTest.svc.cs at the Solution Explorer and start debugging.
	[ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class ServiceTest : IServiceTest
    {
        public IList<User> GetTasksByUserId(long userId, IEnumerable<string> includeList)
        { 
            using (var dbContext = new GERPContext())
            {
                var result = (from t in dbContext.Users
                                  .Include(o=>o.Code) 
                                  .Where(t => t.UserId == 1)
                              select t)
                    .ToList();
                return result;
            }
        }

        public void AddRecord()
        {
            using (var db = new KPIS.GERP.Models.GERPContext())
            {
                var newReceived = new KPIS.GERP.Models.ICM.Received();
                newReceived.ReceiveCode = "R001";
                newReceived.ReceiveName = "NameAAAA";
                newReceived.ReceiveTypeId = 1;
                newReceived.ReceiveDate = DateTime.Now;
                newReceived.DnId = 1;
                newReceived.WhId = 1;
                newReceived.RefNo = "1";
                newReceived.Remark = "REMARKBBBBB";
                newReceived.RecordStatus = "U";
                newReceived.CreatedBy = 1;
                newReceived.CreatedWhen = DateTime.Now;

                db.Receiveds.Add(newReceived);
                db.SaveChanges();

            }
        }

        public IEnumerable<KPIS.GERP.Models.ICM.Received> SelRecord()
        {
            List<Received> data = new List<Received>();
            var db = new KPIS.GERP.Models.GERPContext();
            {
                db.Configuration.ProxyCreationEnabled = false;
                db.Configuration.LazyLoadingEnabled = false;
                data = db.Receiveds.ToList();
            }  

            db = new KPIS.GERP.Models.GERPContext();
            foreach (Received t in data)
            {
                t.Warehouse = db.Receiveds.Find(t.ReceiveId).Warehouse;
            }
            return (data);

        }


    }
}
